package com.longg.dao;

import com.longg.comm.Comm;
import com.longg.dao.base.BaseDao;
import com.longg.model.Conference;
import com.longg.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class MeetingDao extends BaseDao<Conference> {
    public MeetingDao() {
        super(Conference.class);
    }

    public List<Map<String, Object>> findMeetingsByPage(int currentPage, String name,String deptName) {
        String where = " where 1 = 1 ";
        if(null != name && !"".equals(name)){
            where += " and title like ? ";
        }
        if(null != deptName && !"".equals(deptName)){
            where += " and dept_name = ? ";
        }
        String sql = "select * from conference" + where +"limit ?,?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = null;
        try {
            if(null != name && !"".equals(name) && null != deptName && !"".equals(deptName)){
                list = queryRunner.query(sql,new MapListHandler(),"%" + name + "%",deptName,(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }else if ((null == name || "".equals(name)) && null != deptName && !"".equals(deptName)){
                list = queryRunner.query(sql,new MapListHandler(),deptName,(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }else if(null != name && !"".equals(name) && (null == deptName || "".equals(deptName))){
                list = queryRunner.query(sql,new MapListHandler(),"%" + name + "%",(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }else{
                list = queryRunner.query(sql,new MapListHandler(),(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public long getCount(String name, String deptName) {
        String where = " where 1 = 1 ";
        if(null != name && !"".equals(name)){
            where += " and title like ? ";
        }
        if(null != deptName && !"".equals(deptName)){
            where += " and dept_name = ? ";
        }
        String sql = "select count(*) from conference" + where;
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        long i = 0;
        try {
            if(null != name && !"".equals(name) && null != deptName && !"".equals(deptName)){
                i = queryRunner.query(sql,new ScalarHandler<Long>(),"%" + name + "%",deptName);
            }else if ((null == name || "".equals(name)) && null != deptName && !"".equals(deptName)){
                i = queryRunner.query(sql,new ScalarHandler<Long>(),deptName);
            }else if(null != name && !"".equals(name) && (null == deptName || "".equals(deptName))){
                i = queryRunner.query(sql,new ScalarHandler<Long>(),"%" + name + "%");
            }else{
                i = queryRunner.query(sql,new ScalarHandler<Long>());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    public Map<String, Object> findMeetDetail(int cid) {
        String sql = "select c.title,c.content,c.dept_name,c.publish_date,(select count(*) from con_join cj where cj.c_id = c.id)  as should,(select count(*) from con_join cj where cj.c_id = c.id and cj.`status` = 1) as arrive\n" +
                "from conference c\n" +
                "where c.id = ?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> query = null;
        try {
            query = queryRunner.query(sql, new MapHandler(), cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
}
